layui.use(['form', 'layer', 'laydate', 'table', 'laytpl'], function () {
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laydate = layui.laydate,
        laytpl = layui.laytpl,
        table = layui.table;

    //新闻列表
    var tableIns = table.render({
        elem: '#nodeList',
        // url : '/json/newsList.json',
        url: '/node/list',
        cellMinWidth: 95,
        page: true,
        height: "full-125",
        limit: 10,
        limits: [10, 20],
        id: "nodeListTable",
        cols: [[
            {type: "checkbox", fixed: "left", width: 50},
            {field: 'nodeAddress', title: '节点地址', align: "center"},
            {
                field: 'status', title: '状态', align: "center", templet: function (d) {
                    if (d.status == null || d.status === 'DOWN') {
                        return "<i style='color: red'>" + "DOWN" + "<i>";
                    } else
                        return "<i style='color: #1aa094'>" + "UP" + "<i>";
                }
            },
            {
                title: '操作', width: 170, fixed: "right", align: "center", templet: function (d) {
                    if (d.status == null || d.status === 'DOWN') {
                        return " <a class='layui-btn layui-btn-xs layui-btn-normal' lay-event='up'>" + "上线" + "</a>"
                    } else {
                        return " <a class='layui-btn layui-btn-xs layui-btn-danger' lay-event='down'>" + "下线" + "</a>"
                    }
                }
            }
        ]]
    });

    //列表操作
    table.on('tool(nodeList)', function (obj) {
        var layEvent = obj.event,
            data = obj.data;
        // console.log(data)
        let nodeAddress = data.nodeAddress;

        if (layEvent === 'up') { //编辑
            $.post('/node/up', {node: nodeAddress}, function (res) {
                layer.msg(res.msg)
                tableIns.reload();
            }, "json")
        } else if (layEvent === 'down') { //删除
            $.post('/node/down', {node: nodeAddress}, function (res) {
                layer.msg(res.msg);
                tableIns.reload();
            }, "json")
        }
    });

})